package com.hzit.user.controller;

import com.hzit.common.utils.PhoneFormatCheckUtils;
import com.hzit.common.utils.R;
import com.hzit.user.entity.User;
import com.hzit.user.service.UserSerice;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：在这里找 GROUP_HZIT_USER + 4866240f-4fae-4b68-ba4b-13571ffb29c4 + hzit-user.yml --->user.username和user .age
 */
@Controller
@RefreshScope
@Api(tags = "用户管理模块")
public class UserController {
	@Autowired
	private UserSerice userSerice;
	@Value("${user.username}")
	private String username;
	@Value("${user.age}")
	private int age;

	//1. 欢迎页面
	@ApiOperation(value = "欢迎页面")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "model",value = "模型对象"),
			@ApiImplicitParam(name = "request",value = "请求对象")
	})
	@GetMapping({"/","/register.html"})
	public String register(Model model, HttpServletRequest request){
		XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
		model.addAttribute("user",xxlUser);
		return "register";
	}

	//2. 发送手机验证码
	@GetMapping("/user/sendCode")
	@ApiOperation("发送手机验证码")
	//paramType: query(？号传参) | path(路径参数) | body(请求体)
	@ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "string",paramType = "query")
	public String sendCode(String phone){
		userSerice.sendCode(phone);
		return "register";
	}

	//查询所有用户
	@GetMapping("/user/list")
	@ResponseBody
	@ApiOperation("用户列表")
	public R list(){
		List<User> userList = userSerice.findAll();
		return R.ok().put("list",userList);
	}
	//3. 保存用户
	@PostMapping("/user/save")
	@ApiOperation("保存用户")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "user",value = "要保存的用户对象",required = true,dataType = "user",paramType = "query" ),
			@ApiImplicitParam(name = "code",value = "输入的验证码",required = true,dataType = "string",paramType = "query" )
	})
	public String save(User user,String code){
		//3.1 校验手机号
		if(PhoneFormatCheckUtils.isPhoneLegal(user.getPhone())){
			//3.1.1 校验验证码
			if(userSerice.validCode(code,user.getPhone())){ // 校验通过
				//此时添加用户
				userSerice.add(user);
			}
		}
		return "register";
	}

	//4. 打印用户信息
	@GetMapping("/getUserInfo")
	@ResponseBody
	@ApiOperation("获取用户信息")
	public R getUserInfo(){
		return R.ok().put("username",username).put("age",age);
	}

	//5. 根据用户id查询用户
	@GetMapping("/user/findById/{id}")
	@ResponseBody
	public R findById(@PathVariable String id){
		User user = userSerice.findById(id);
		return R.ok().put("user",user);
	}



}
